package com.stadium.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.stadium.common.BaseResponse;
import com.stadium.dto.GameScheduleDto;
import com.stadium.service.GameScheduleSV;
import com.stadium.vo.GameScheduleVo;
import com.stadium.vo.searchVo.GameScheduleSearchVo;
import io.micrometer.core.instrument.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 赛程安排表
 *
 * @author wsl
 * @email
 * @date 2021-05-08 13:42:21
 */
@RestController
@RequestMapping("/api/gameschedule")
@Api(tags = "赛程安排表")
public class GameScheduleCL{
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private GameScheduleSV gameScheduleSV;

    @ApiOperation(value = "保存或修改")
    @PostMapping(value = "/v1/save")
    public BaseResponse save(@Validated GameScheduleVo gameScheduleVo) {
        try {
            if (gameScheduleVo.getGuestteamId().equals(gameScheduleVo.getHometeamId())) {
                return BaseResponse.error("主客队不可以选择同一个队伍");
            }
            if (gameScheduleVo.getId() == null ||gameScheduleVo.getId() == ""){
                    gameScheduleSV.save(gameScheduleVo);
            }else{
                    gameScheduleSV.update(gameScheduleVo);
            }
            return BaseResponse.ok();

        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "删除")
    @PostMapping(value = "/v1/delete")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse delete(@RequestParam(name = "id", required = true)String id) {
        try {
            gameScheduleSV.delete(id);
            return BaseResponse.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error();
        }
    }


    @ApiOperation(value = "获得列表")
    @GetMapping(value = "/v1/query")
    public BaseResponse<List<GameScheduleDto>> query(@Validated GameScheduleSearchVo searchVo) {
        try {

            if (searchVo.getIsPad()!=null&&searchVo.getIsPad().equals("1")&&searchVo.getStartDate()==null&&searchVo.getEndDate()==null){
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String timeStr = sdf.format(new Date());
                String timeStrt = timeStr.substring(0,10)+" 00:00:00";
                try {
                    Date date = sdf.parse(timeStrt);
                    Calendar startCalendar = Calendar.getInstance();
                    startCalendar.setTime(date);
                    startCalendar.add(Calendar.DAY_OF_YEAR,-1);
                    Date startDate = startCalendar.getTime();
                    startCalendar.add(Calendar.DAY_OF_YEAR,2);
                    Date endDate = startCalendar.getTime();
                    searchVo.setStartDate(startDate);
                    searchVo.setEndDate(endDate);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            if (StringUtils.isNotEmpty(searchVo.getSearchValue())) {
                searchVo.setSearchValue("%" + searchVo.getSearchValue() + "%");
            }
            if (StringUtils.isEmpty(searchVo.getEventId())) {
                return BaseResponse.error("请选择一个赛事");
            }
            List<GameScheduleDto> queryList = gameScheduleSV.query(searchVo);
            int total = gameScheduleSV.getCount(searchVo);
            return BaseResponse.ok(queryList, total);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }

    @ApiOperation(value = "获得详情", notes = "获得详情")
    @GetMapping(value = "/v1/get")
    @ApiImplicitParams({@ApiImplicitParam(name="id",value="id")})
    public BaseResponse<GameScheduleDto> get(@RequestParam(name = "id", required = true)String id) {
        try {
                GameScheduleDto queryData = gameScheduleSV.get(id);
            return BaseResponse.ok(queryData);
        } catch (Exception e) {
            logger.error("日志失败！", e);
            return BaseResponse.error();
        }
    }
}
